package springboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
/**
* RabbitMQ配置类
 交换机与队列之间有一个路由key,这个路由key决定着交换机的类型
* 1.声明交换机
* 2.声明队列(声明死信交换机以及消息的存活时间ttl time to live)
* 3.声明绑定关系
 * 发消息有消息接收端来接收
* */
@Configuration
public class RabbitMQConfig {
    //两个交换机的名称
    public static final String X_EXCHANGE="X";
    public static final String Y_EXCHANGE="Y";
    //三个队列的名称
    public static final String QUEUE_A="QA";
    public static final String QUEUE_B="QB";
    public static final String QUEUE_D="QD";
    /*该队列的延时由发布者确定*/
    public static final String QUEUE_C="QC";
    /*声明交换机*/
    @Bean("x_exchange")
    public DirectExchange x_exchange(){
        return new DirectExchange(X_EXCHANGE);
    }
    @Bean("y_exchange")
    public DirectExchange y_exchange(){
        return new DirectExchange(Y_EXCHANGE);
    }
    /*声明队列*/
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments=new HashMap<>();
        /*以下这两个入参 第一个表示哪一个是死信交换机,以及去往该交换机的routing-key*/
        arguments.put("x-dead-letter-exchange",Y_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        /*该参数表示该消息在此队列中存活的时间,超过十秒钟就发送到死信队列中去*/
        arguments.put("x-message-ttl",10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange",Y_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(QUEUE_D).build();
    }

    @Bean("queueC")
    public Queue queueC(){
        Map<String, Object> arguments=new HashMap<>();
        /*发生消息延时时，将消息发给死信交换机以及对应的routing key*/
        //声明当前队列的死信路由 key
        arguments.put("x-dead-letter-exchange",Y_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        /*这里不进行消息的延时*/
        //此处的延时由发布者确定
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }
    /*交换机与队列之间的绑定*/
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("x_exchange") DirectExchange x_exchange){
        return BindingBuilder.bind(queueA).to(x_exchange).with("XA");
    }
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("x_exchange") DirectExchange x_exchange){
        return BindingBuilder.bind(queueB).to(x_exchange).with("XB");
    }
    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("y_exchange") DirectExchange y_exchange){
        return BindingBuilder.bind(queueD).to(y_exchange).with("YD");
    }
    @Bean
    public Binding queueCBindingY(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("x_exchange")DirectExchange x_exchange){
        return BindingBuilder.bind(queueC).to(x_exchange).with("XC");
    }

}
